package com.whh.starter.config;

public class SystemException extends RuntimeException {

    private static final long serialVersionUID = 1L;
    private final CodeMapper resultCodeMapper;
    private final int subCode;

    public SystemException(String message, CodeMapper resultCodeMapper, int subCode) {
        super(message);
        this.resultCodeMapper = resultCodeMapper;
        this.subCode = subCode;
    }

    public SystemException(String message, CodeMapper resultCodeMapper) {
        super(message);
        this.resultCodeMapper = resultCodeMapper;
        this.subCode = 0;
    }

    public SystemException(CodeMapper resultCodeMapper) {
        super(resultCodeMapper.name());
        this.resultCodeMapper = resultCodeMapper;
        this.subCode = 0;
    }

    public int errorCode() {
        if (subCode > 0) {
            return resultCodeMapper.getCode() * 100 + subCode;
        }
        return resultCodeMapper().getCode();
    }

    public CodeMapper resultCodeMapper() {
        return resultCodeMapper;
    }
}
